This is a Protocol Buffers hello world project following https://developers.google.com/protocol-buffers/docs/javatutorial
you may find all source code from github: https://github.com/guoliang-dev/google-protocol-buffers-java-helloworld
Code Generation
The message definition: addressbook.proto;
D:\dev\tools\protoc-2.5.0-win32>protoc.exe -I=D:\dev\..\src\main\resosurce\ --java_out=D:\dev..\src\main\java D:\dev\…\resosurce\addressbook.proto
AddressBookProtos.java is the generated Java class;
TestCase:
package com.liguoliang.java.protobuf;
public class ProtocolBuffersTest {
private static final String name = "google";@Test
public void testBuildInstance() throws Exception {
Person person = buildNewPerson();
assertEquals(name, person.getName());
}@Test
public void testWriteInstanceToFile() throws Exception {
String path = "./src/test/resources/address.txt";
AddressBook.Builder addressBookBuilder = AddressBook.newBuilder();
addressBookBuilder.addPerson(buildNewPerson());
AddressBook addressBook = addressBookBuilder.build();
FileOutputStream outputStream = new FileOutputStream(path);
addressBook.writeTo(outputStream);
outputStream.close();
System.out.println("AddressBook write to :\n " + path);
URL url = Resources.getResource("./address.txt");
String contentInFile = Resources.toString(url, Charsets.UTF_8);
assertTrue(contentInFile.contains(name));
}
@Test
public void testParseInstanceFromFile() throws FileNotFoundException, IOException {
AddressBook.Builder addressBookBuilderRead = AddressBook.newBuilder();
addressBookBuilderRead.mergeFrom(new FileInputStream("./src/test/resources/address-test.txt"));
AddressBook addressBookRead = addressBookBuilderRead.build();
System.out.println("AddressBook loaded: \n" + addressBookRead.toString());
assertEquals(1, addressBookRead.getPersonCount());
Person person = addressBookRead.getPerson(0);
assertEquals(name, person.getName());
}
private Person buildNewPerson() {
Person.Builder personBuilder = Person.newBuilder();
personBuilder.setId(1);
personBuilder.setName(name);
Person person = personBuilder.build();
return person;
}}
MarkLogic: select all collections & select latest record from collection <->
// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.